package com.liddhome.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.liddhome.entity.Blog;
import com.liddhome.entity.Comment;
import com.liddhome.entity.Lifethings;
import com.liddhome.entity.Oldtimes;
import com.liddhome.service.BlogService;
import com.liddhome.service.CommentService;
import com.liddhome.service.LifethingService;
import com.liddhome.service.OldtimeService;
import com.liddhome.util.DateUtil;
import com.liddhome.util.ResponseUtil;
import com.liddhome.util.StringUtil;

import net.sf.json.JSONObject;

/**
 * 评论Controller层
 * @author lidd
 *
 */
@Controller
@RequestMapping("/comment")
public class CommentController {
	
	@Resource
	private CommentService commentService;
	
	@Resource
	private BlogService blogService;
	@Resource
	private OldtimeService oldtimeService;
	@Resource
	private LifethingService lifethingService;
	
	/**
	 * 添加或者修改评论
	 * @param comment
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save")
	public String save(Comment comment,@RequestParam("imageCode") String imageCode,HttpServletRequest request,HttpServletResponse response,HttpSession session)throws Exception{
		String sRand=(String) session.getAttribute("sRand"); // 获取系统生成的验证码
		JSONObject result=new JSONObject();
		int resultTotal=0; // 操作的记录条数
		if(!imageCode.equals(sRand)){
			result.put("success", false);
			result.put("errorInfo", "验证码填写错误！");
		}else if(StringUtil.isEmpty(comment.getNickName())){
			result.put("success", false);
			result.put("errorInfo", "请填写您的昵称！");
		}else if(StringUtil.isEmpty(comment.getContent())) {
			result.put("success", false);
			result.put("errorInfo", "您未填写评论内容！");
		}else{
			String userIp=request.getRemoteAddr(); // 获取用户IP
			comment.setUserIp(userIp);
			if(comment.getId()==null){
				comment.setId(DateUtil.getCurrentDateStr());
				resultTotal=commentService.add(comment);
				// 该博客的回复次数加1
				updateBlog(getCommentBlog(comment));
			}else{
				
			}
			if(resultTotal>0){
				result.put("success", true);
			}else{
				result.put("success", false);
			}
		}
		ResponseUtil.write(response, result);
		return null;
	}

	private Blog getCommentBlog(Comment comment) {
		Blog blog = blogService.findById(comment.getBlog().getId());
		if(blog == null) {
			blog = oldtimeService.findById(comment.getBlog().getId());
		}
		blog = blog==null?lifethingService.findById(comment.getBlog().getId()):blog;
		blog.setReplyHit(blog.getReplyHit()+1);
		return blog;
	}
	
	private Integer updateBlog(Blog blog) {
		if(blog instanceof Oldtimes) {
			return oldtimeService.update((Oldtimes) blog);
		}else if(blog instanceof Lifethings) {
			return lifethingService.update((Lifethings) blog);
		}else {
			return blogService.update(blog);
		}
	}
}
